<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>图书-公告管理</title>
    <link rel="icon" href="/static/images/t_logo.ico" type="image/x-icon"/>
    <link rel="stylesheet" href="/static/css/lyk-global.css" />
    <link rel="stylesheet" href="/static/elementui/lib/theme-chalk/index.css">
    <script src="/static/js/jquery.min.js" type="text/javascript"></script>
    <script src="/static/js/requestApi.js" type="text/javascript"></script>
    <script src="/static/js/vue/vue.js"></script>
    <!-- 引入elementui -->
    <script src="/static/elementui/js/element.js"></script>
    <script src="/static/js/axios.min.js"></script>
    <script src="/static/js/axios-config.js"></script>
    <script src="/static/js/utilJs.js"></script>
    <!-- 配置文件 -->
    <script type="text/javascript" src="/static/vue/components/ueditor/ueditor.config.js"></script>
    <!-- 编辑器源码文件 -->
    <script type="text/javascript" src="/static/vue/components/ueditor/ueditor.all.js"></script>
    <script src="/static/vue/components/ueditorTemplate.js"></script>
</head>
<style>
    *{
        font-size: 14px
    }
    [v-cloak] {
        display: none;
    }
</style>
<body style="background-color: #fff">
<div id="app" v-cloak>
    <div class="mt-8 ml-4">
        <div class="df-aic ">
            <el-input :placeholder="'请输入' + noticeTitle + '标题'" size="small" style="width:200px;" class="ml-2" v-model="queryParam.topic" @keyup.native.enter="getTableDataMethod(true)"></el-input>
            <el-date-picker size="small"
                            v-model="dateSelectValue"
                            type="daterange"
                            range-separator="至"
                            start-placeholder="发布开始时间"
                            end-placeholder="发布结束时间" class="ml-2" value-format="yyyy-MM-dd">
            </el-date-picker>

            <el-button type="primary" size="small" class="ml-2" @click="getTableDataMethod(true)">搜索</el-button>
            <el-button type="primary" size="small" class="ml-2" @click="addOrEditNoticeMethod('新增' + noticeTitle )">新增{{noticeTitle}}</el-button>
        </div>
        <el-table :data="tableData" v-loading="tableDataLoading" class="mt-4">
            <el-table-column prop="topic" :label="noticeTitle + '的标题'" width="150"></el-table-column>
            <el-table-column :label="noticeTitle + '的内容'">
                <template slot-scope="scope">
                  <div style="height:200px;">
                      {{scope.row.content}}
                  </div>
                </template>
            </el-table-column>
            <el-table-column prop="publishTime" label="发布时间"></el-table-column>
            <el-table-column label="状态">
                <template slot-scope="scope">
                    <el-tag type="info" v-if="scope.row.state === 0 ">待发布</el-tag>
                    <el-tag type="success" v-if="scope.row.state === 1 ">已发布</el-tag>
                </template>
            </el-table-column>

            <el-table-column label="操作">
                <template slot-scope="scope">
                    <div v-loading="deleteLoading">
                        <el-button size="small" class="mt-2" @click="publishNotice(scope.row)" v-if="scope.row.state === 0">发布</el-button>
                        <el-button size="small" @click="addOrEditNoticeMethod('修改' + noticeTitle,scope.row)" class="mt-2" >编辑</el-button>
                        <el-popconfirm :title='"是否删除该条数据？"'
                                       @confirm="deleteNoticeMethod(scope.row)">
                            <el-button slot="reference" type="danger" size="small" v-loading="deleteLoading"  class="mt-2">删除</el-button>
                        </el-popconfirm>
                    </div>
                </template>
            </el-table-column>
        </el-table>
    </div>

    <el-pagination background layout="total,sizes,prev, pager, next" :total="tableDataPaging.total"
                   :page-sizes="[10, 20, 50]"
                   prev-text="上一页" next-text="下一页"
                   :page-size="tableDataPaging.pageSize"
                   :current-page="tableDataPaging.currentPage"
                   @current-change="tableDataPagingChange"
                   @prev-click="tableDataPagingChange" @next-click="tableDataPagingChange"
                   @size-change="tableDataPagingSizeChange"
                   style="float:right;margin-top: 40px;padding-bottom:50px;">
    </el-pagination>

    <el-dialog :title="addOrEditNoticeDialogTitle" :visible.sync="addOrEditNoticeDialogVisible" width="1200px" :close-on-click-modal="false" append-to-body	>
        <div>
            <div class="mt-8 ml-4">
                <div class="df-aic">
                    <el-form ref="noticeForm" :model="noticeForm" label-width="150px" >
                        <el-form-item :label="noticeTitle + '标题：'" prop="topic"  required>
                            <el-input v-model="noticeForm.topic" style="width:400px;" :placeholder="'请输入' + noticeTitle + '标题'" size="small"></el-input>
                        </el-form-item>
                        <el-form-item :label="noticeTitle + '内容：'" required>
                            <div style="width:80%;">
                                <ueditor-template ueditor-id="noticeContentEditor" ref="noticeContentEditorRef">
                                </ueditor-template>
                            </div>
                        </el-form-item>
                    </el-form>
                </div>
            </div>
        </div>
        <div class="df-aic-jcc mt-9 mb-9">
            <el-button type="primary" size="small" @click="addOrEditNoticeSubmit">确定</el-button>
            <el-button size="small" class="ml-6" @click="addOrEditNoticeDialogVisible = false">取消</el-button>
        </div>
    </el-dialog>

</div>
</body>

<script type="text/javascript">
    const vm = new Vue({ // 配置对象 options
        // 配置选项(option)
        el: '#app', // element: 指定用vue来管理页面中的哪个标签区域
        components: {
        },
        data() {
            return {
                dateSelectValue:"",
                noticeContentEditor:'',
                deleteLoading : false,
                queryParam : {
                    type:"",
                    topic:"",
                },
                tableData : [],
                tableDataLoading : false,
                tableDataPaging : {
                    currentPage : 1,
                    pageSize : 10,
                    total : 0,
                },
                addOrEditNoticeDialogVisible:false,
                addOrEditNoticeDialogTitle:'',
                noticeForm:{
                    id:"",
                    topic:""
                },
                noticeTitle:"",
            }
        },
        mounted() {
            let that = this;
            this.$nextTick(function () {
                let type = getQueryString("type")
                if(type){
                    this.queryParam.type = type;
                }
                if(type == 1){
                    that.noticeTitle = "公告";
                }else if(type == 2){
                    that.noticeTitle = "问题与帮助";
                }
                this.getTableDataMethod();
            });
        },
        methods: {
            addOrEditNoticeMethod(title,row){
                let that = this;
                that.addOrEditNoticeDialogVisible = true;
                this.$nextTick(function () {
                    that.addOrEditNoticeDialogTitle = title;
                    this.$refs['noticeForm'].resetFields();//还原数据
                    if(row){
                        let deepCopyRow = JSON.parse(JSON.stringify(row));
                        that.noticeForm = deepCopyRow;//深拷贝一下
                        setTimeout(function(){
                            that.$refs.noticeContentEditorRef.setContent(deepCopyRow.content);
                        }, 800);
                    }
                });
            },
            //新增或修改通告的提交的方法
            addOrEditNoticeSubmit(){
                let that = this;
                this.$refs["noticeForm"].validate((valid) => {
                    if (valid) {
                        let deepCopyForm = JSON.parse(JSON.stringify(that.noticeForm));
                        deepCopyForm.content = removeFullTextUseLess(that.$refs.noticeContentEditorRef.getContent());
                        deepCopyForm.type = that.queryParam.type;
                        if(!deepCopyForm.content){
                            that.$message.error("请填写活动详情");
                            return;
                        }

                        let url = "";
                        if (!deepCopyForm.id) {
                            url = "notice/addNotice";
                        } else {
                            url = "notice/updateNotice";
                        }
                        requestApiTB(url, deepCopyForm, 'POST', function (res) {
                            if (res.code === 0) {
                                that.$message.success("执行成功");
                                if (!that.noticeForm.id) {
                                    that.$refs['noticeForm'].resetFields();//重置字段
                                }
                                setTimeout(function(){
                                    that.addOrEditNoticeDialogVisible = false;
                                    that.getTableDataMethod();
                                }, 1000);
                            } else {
                                that.$message.error(res.msg);
                            }
                        });
                    } else {
                        that.$message.error("表单校验不通过");
                        return false;
                    }
                });
            },

            tableDataPagingChange(val) {
                let that = this;
                that.tableDataPaging.currentPage = val;
                that.getTableDataMethod();
            },
            tableDataPagingSizeChange(val) {
                let that = this;
                that.tableDataPaging.pageSize = val;
                that.getTableDataMethod();
            },

            //获取表格的数据
            getTableDataMethod(isChongZhi){
                let that = this;
                if (isChongZhi) {
                    this.tableDataPaging.currentPage = 1;
                }
                const {topic,type} = this.queryParam
                let queryData = {
                    topic: $.trim(topic),
                    type: $.trim(type),
                    pageNum: this.tableDataPaging.currentPage,
                    pageSize: this.tableDataPaging.pageSize,
                }
                if(that.dateSelectValue){
                    queryData.pubulishStartTime = that.dateSelectValue[0];
                    queryData.pubulishEndTime = that.dateSelectValue[1];
                }
                this.tableDataLoading = true;
                that.tableData = [];
                axios.post('/notice/getNoticeList', queryData)
                    .then(result => {
                        let res = result.data;
                        this.tableDataLoading = false;
                        if (res.code === 0) {
                            that.tableData = res.data.list;
                            that.tableDataPaging.total = res.data.total;
                        }
                    }).catch(error => {
                    that.$message.error("error");
                    this.tableDataLoading = false
                })
            },
            deleteNoticeMethod(row){
                let that = this;
                if(!row.id){
                    that.$message.error("尚无id");
                    return;
                };
                that.deleteLoading = true;
                requestApi("notice/delNotice",{"id" : row.id}, "POST", function(res){
                    that.deleteLoading = false;
                    if(res.code === 0){
                        that.$message.success("删除成功");
                        that.getTableDataMethod(true);
                    }else{
                        that.$message.error(res.msg);
                    }
                });
            },

            //发布活动
            publishNotice(row){
                let that = this;
                if(!row.id){
                    that.$message.error("尚无id");
                    return;
                };
                requestApi("notice/publishNotice",{"id" : row.id}, "POST", function(res){
                    that.deleteLoading = false;
                    if(res.code === 0){
                        that.$message.success("发布成功");
                        that.getTableDataMethod(true);
                    }else{
                        that.$message.error(res.msg);
                    }
                });
            },





        }
    })
</script>
</html>